package Datastruct.Kruskal.prim;

import java.util.Arrays;

public class Mintree {
    public void crateGraph(MGraph mGraph, int verxs, char[] data, int[][] weight) {

        for (int i = 0; i < verxs; i++) {
            mGraph.data[i] = data[i];
            for (int j = 0; j < verxs; j++) {
                mGraph.weight[i][j] = weight[i][j];
            }
        }
    }

    //查看邻接矩阵
    public void showGraph(MGraph graph) {
        for (int[] data : graph.weight) {
            System.out.println(Arrays.toString(data));
        }
    }

    public void prim(MGraph graph, int v) {
        //标记被访问过的  0表示没有访问，1表示访问过
        int[] visied = new int[graph.verx];
        visied[v] = 1;

        int x = -1;
        int y = -1;

        int minweight = 10000;
        //最小生成树 边=顶点-1
        for (int i = 1; i < graph.verx; i++) {
            //被访问过
            for (int j = 0; j < graph.verx; j++) {
                //没有被访问过
                for (int k = 0; k < graph.verx; k++) {
                    if (visied[j] == 1 && visied[k] == 0 && graph.weight[j][k] < minweight) {
                        minweight = graph.weight[j][k];
                        x = j;
                        y = k;
                    }
                }
            }
            System.out.println(graph.data[x] + "----->" + graph.data[y] + "=" + minweight);
            visied[y] = 1;
            minweight = 10000;

        }

    }
}
